import re

# 小题1
def repeat_words(file):
    name = file.split('.')[0]       # 诗人名字
    with open(file, encoding='utf8') as f:
        poems_text = f.readlines()
    if name == '杜甫':
        poems_text[-1] += '\n'
    poems = list(map(lambda x: re.findall(r'.*?%s\s(.*?)\n' % name, x)[0], poems_text))
    poem_num = len(poems)   # 诗歌篇数
    poems_text = ''.join(poems)
    words = re.findall(r'[\u4e00-\u9fa5]', poems_text, re.S)
    data = {}
    for word in words:
        if not word in data:
            data[word] = 1
        else:
            data[word] += 1
    # print('%s诗词中总共有%d个汉字，其中不重复汉字有%d个，具体如下' % (name, len(words), len(data)))
    # print_words(data)
    return data, poem_num


# 小题2
def frequence(data, poem_num):
    dic = {}
    for word in data:
        dic[word] = data[word]/poem_num
    ls = sorted(dic.items(), key=lambda item: item[1], reverse=True)
    high_words = set()
    # print('\n汉字---频率')
    for i in ls:
        if i[1] > 0.1:
            # print('%s  %.8f' % i)
            high_words.add(i[0])
    return high_words


# 小题3
def combine(data1, data2, poem_num, set1, set2):
    # 合并
    for word in data2:
        if word in data1:
            data1[word] += data2[word]
        else:
            data1[word] = data2[word]
    # 筛选字频
    ls = []
    for item in data1.items():
        if item[1]/poem_num > 0.1:
            ls.append(item)
    # 取得高频词集合
    high_frequence = set(map(lambda x: x[0], ls))
    # print(high_frequence)
    s1 = set1 & set2    # 都爱用
    print('两位诗人都爱用的字有%d个，这些字为:' % len(s1))
    print_words(s1)
    s2 = set1 - set2  # 李白爱杜甫不爱
    print('\n李白爱用但杜甫不爱用的字有%d个，这些字为:' % len(s2))
    print_words(s2)
    s3 = set2 - set1  # 杜甫爱李白不爱
    print('\n杜甫爱用但李白不爱用的字有%d个，这些字为:' % len(s3))
    print_words(s3)

def print_words(data):
    count = 0
    for i in data:
        count += 1
        if count % 10 == 0:
            print(i)
        else:
            print(i, end='  ')


if __name__ == '__main__':
    file1 = '李白.txt'
    file2 = '杜甫.txt'
    # 李白的
    data1, poem_num1 = repeat_words(file1)
    high_words1 = frequence(data1, poem_num1)
    # 杜甫的
    data2, poem_num2 = repeat_words(file2)
    high_words2 = frequence(data2, poem_num2)
    # 第3小题
    combine(data1, data2, poem_num1+poem_num2, high_words1, high_words2)